import { ConfigEnv, defineConfig, loadEnv, UserConfig } from 'vite';
import vue from '@vitejs/plugin-vue';


import path from 'path';

function pathResolve(dir: string) {
  return path.resolve(process.cwd(), '.', dir);
}

// svg图标
// import { svgBuilder } from './src/plugins/svgBuilder';

// 按需导入组件
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import vueJsx from '@vitejs/plugin-vue-jsx' // jsx
import { createVitePlugins } from './build/vite/plugin';
import { wrapperEnv } from './build/utils';




// https://vitejs.dev/config/
export default defineConfig(({ command, mode }: ConfigEnv): UserConfig => {

  const root = process.cwd();

  const env = loadEnv(mode, root);

  // The boolean type read by loadEnv is a string. This function can be converted to boolean type
  const viteEnv = wrapperEnv(env);

  const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE } = viteEnv;

  const isBuild = command === 'build';

  return {
    resolve: {
      // 配置别名
      alias: {
        "~/": `${pathResolve("src")}/`,
        "@/": `${pathResolve("src")}/`,
        "/#/": `${pathResolve('types')}/`,
        'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
      },
    },
    base: env.VITE_PUBLIC_PATH,
    build: {
      // outDir:''
      chunkSizeWarningLimit: 1024
    },
    // 后台服务地址
    server: {
      host: '0.0.0.0',
      port: 8080,
      strictPort: false,
      proxy: {
        '^/api/.*': {
          target: 'http://127.0.0.1:8090/api/',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "~/styles/element/index.scss" as *;`
        },
      },
    },
    plugins: [...createVitePlugins(viteEnv, isBuild), vue(), vueJsx(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
      // 另一种svg使用方式
      // svgBuilder('./src/assets/icons/svg/'),
    ],
  }

})

